package be.bejug.scrumadmin.components.panels;

import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;

import be.bejug.scrumadmin.domain.Sprint;
import be.bejug.scrumadmin.domain.Story;
import be.bejug.scrumadmin.domain.Task;
import be.bejug.scrumadmin.domain.User;
import be.bejug.scrumadmin.domain.enums.Priority;
import be.bejug.scrumadmin.services.SprintService;
import be.bejug.scrumadmin.spring.Injector;

import com.vaadin.addon.treetable.TreeTable;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.NativeSelect;

/**
 * 
 * @author Glenn Dejaeger
 * 
 */
@SuppressWarnings("serial")
public class SprintListPanel extends AbstractTab implements
		Property.ValueChangeListener {

	@Autowired
	private transient SprintService sprintService;

	private TreeTable sprintTable;

	private NativeSelect sprintSelect;

	/**
	 * 
	 */
	public SprintListPanel() {
		Injector.inject(this);
		initComponents();
	}

	/**
	 * 
	 */
	@Override
	public void loadData() {
		Set<Sprint> sprints = new TreeSet<Sprint>(sprintService.getAllSprints());
		if (sprints != null) {
			for (Sprint sprint : sprints) {
				sprintSelect.addItem(sprint);
			}
		}
	}

	@Override
	protected String getCaptionKey() {
		return "panel.title.list";
	}

	protected void initComponents() {
		initSprintsComboBox();
		initSprintTable();
	}

	private void initSprintTable() {
		sprintTable = new TreeTable();

		sprintTable.addContainerProperty("Name", String.class, null);
		sprintTable.addContainerProperty("Description", String.class, null);
		sprintTable.addContainerProperty("Priority", Priority.class, null);
		sprintTable.addContainerProperty("Estimation", Double.class, null);
		sprintTable.addContainerProperty("Work left", Double.class, null);
		sprintTable.addContainerProperty("Assigned to", User.class, null);

		sprintTable.setSelectable(true);
		sprintTable.setColumnReorderingAllowed(true);
		sprintTable.setSizeFull();

		addComponent(sprintTable);

		sprintTable.setSelectable(true);
		sprintTable.setColumnReorderingAllowed(true);
		sprintTable.setSizeFull();
		setExpandRatio(sprintTable, 1.0f);
	}

	private void initSprintsComboBox() {
		sprintSelect = new NativeSelect();
		sprintSelect.setImmediate(true);
		sprintSelect.addListener(this);

		addComponent(sprintSelect);
	}

	@Override
	public void valueChange(final ValueChangeEvent event) {
		sprintTable.removeAllItems();

		Sprint sprint = (Sprint) event.getProperty().getValue();
		if (sprint != null) {
			addStoriesToTable(sprint);
		}
	}

	/**
	 * @param sprint
	 */
	private void addStoriesToTable(final Sprint sprint) {
		sprintTable.removeAllItems();

		Set<Story> stories = new TreeSet<Story>(
				sprintService.getStories(sprint));
		for (Story story : stories) {
			Object storyItemId = sprintTable.addItem(
					new Object[] { story.getName(), story.getDescription(),
							story.getPriority(), story.getEstimation(), null,
							null }, story);
			for (Task task : story.getTasks()) {
				Object taskItemId = sprintTable.addItem(
						new Object[] { task.getName(), task.getDescription(),
								task.getPriority(), task.getEstimation(),
								task.getWorkLeft(), task.getAssignedTo() },
						task);
				sprintTable.setParent(taskItemId, storyItemId);
			}
		}
	}
}
